package dal;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import bean.Project;

public class ProjectDAL
{
  public Project get(int projectId)
  {
    Project project = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = null;
    try
    {
      transaction = session.beginTransaction();
      project = (Project) session.load(Project.class, projectId);
    }
    catch (HibernateException he)
    {
      if (transaction != null)
      {
        transaction.rollback();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return project;
  }

  @SuppressWarnings("unchecked")
  public List<Project> find(String criteria)
  {
    List<Project> projects = new ArrayList<Project>();
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = null;
    try
    {
      StringBuilder sb = new StringBuilder();
      sb.append(" FROM Project ");
      if (criteria != null && criteria.trim().length() > 0)
      {
        sb.append(" WHERE UPPER(name) LIKE '" + "%" + criteria.toUpperCase() + "%' ");
      }
      sb.append(" ORDER BY name ASC ");
      transaction = session.beginTransaction();
      projects = session.createQuery(sb.toString()).list();
      transaction.commit();
    }
    catch (HibernateException he)
    {
      if (transaction != null)
      {
        transaction.rollback();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return projects;
  }

  public Project save(Project project)
  {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = null;
    try
    {
      transaction = session.beginTransaction();
      session.saveOrUpdate(project);
      transaction.commit();
    }
    catch (HibernateException he)
    {
      if (transaction != null)
      {
        transaction.rollback();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return project;
  }

  public void delete(Project project)
  {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = null;
    try
    {
      transaction = session.beginTransaction();
      session.delete(project);
      transaction.commit();
    }
    catch (HibernateException he)
    {
      if (transaction != null)
      {
        transaction.rollback();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public int count()
  {
    int count = 0;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = null;
    try
    {
      transaction = session.beginTransaction();
      count = ((Long) session.createQuery("SELECT COUNT(*) FROM Project").uniqueResult())
          .intValue();
      transaction.commit();
    }
    catch (HibernateException he)
    {
      if (transaction != null)
      {
        transaction.rollback();
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return count;
  }

}
